package com.atstudy.service;

import com.atstudy.dao.AccountMapper;
import com.atstudy.entites.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/1/31 9:23
 * @copyright: 博为峰
 */
public class AccountServiceImpl implements AccountService {
    @Override
    public List<Account> getAccountList() {
        AccountMapper accountMapper = null;
        SqlSession sqlSession = null;
        List<Account> accountList = null;
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //创建会话工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //创建会话对象（一次数据库连接）（SqlSession 的实例不是线程安全的，因此是不能被共享的）
            sqlSession = sqlSessionFactory.openSession();
            //获取接口的实现类对象
            accountMapper = sqlSession.getMapper(AccountMapper.class);
            accountList = accountMapper.selectByExample();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }
        return accountList;
    }
}
